package com.tz.colawallet.ui.activity;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseActivity;
import com.tz.colawallet.basecode.util.SPUtils;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.biz.LoginBiz;
import com.tz.colawallet.ui.component.DaggerLoginComponent;
import com.tz.colawallet.ui.contract.LoginContract;
import com.tz.colawallet.ui.module.LoginModule;
import com.tz.colawallet.ui.presenter.LoginPresenter;

import javax.inject.Inject;

public class LoginActivity extends BaseActivity implements LoginContract.View {

    @Inject
    LoginPresenter presenter;
    @Inject
    LoginBiz biz;

    TextView registerText;
    TextView loginForget;
    EditText phone;
    EditText password;
    Button login;

    @Override
    protected void setup() {
        super.setup();
        DaggerLoginComponent.builder().loginModule(new LoginModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        registerText = findViewById(R.id.register_text);
        loginForget = findViewById(R.id.loginForget);
        phone = findViewById(R.id.phone);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);


        registerText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchActivity(RegisterActivity.class, null, Key.REGISTER);
            }
        });
        loginForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchActivity(RegisterActivity.class, null, Key.FORGET);
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone.getText().toString().isEmpty()) {
                    showToast("请输入手机号");
                    return;
                }
                if (phone.getText().toString().length() != 11) {
                    showToast("请输入正确手机号");
                    return;
                }
                if (password.getText().toString().isEmpty()) {
                    showToast("请输入密码");
                    return;
                }
                presenter.login(phone.getText().toString(), password.getText().toString());
            }
        });

    }

    @Override
    protected void initTitle() {

    }

    @Override
    public Context curContext() {
        return this;
    }

    @Override
    public void loginSuccess(Object o) {
        SPUtils.put(this, Key.TOKEN, (String) o);
        showToast("登录成功");
        if ((boolean) SPUtils.get(this, Key.FIRST_LOGIN, true)) {
            SPUtils.put(this, Key.FIRST_LOGIN, false);
            switchActivity(LoadingActivity.class, null);
        } else {
            switchActivity(MainActivity.class, null);
        }
        finish();
    }
}
